//
//  SettingViewController.m
//  HiMaster3
//
//  Created by xmy on 16/4/15.
//  Copyright © 2016年 jinghao. All rights reserved.
//

#import "SetSettingViewController.h"
#import "SDImageCache.h"
#import "UIImageView+WebCache.h"
#import "ClearCacheTool.h"
@interface SetSettingViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *cacheSize;

@end

@implementation SetSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"设置"];
    self.showLeftMenu =YES;
    self.cacheSize.text = [self getCacheImageSize];
    
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)feedBack:(id)sender {
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]];
    UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"FeedbackViewController"];
    [self pushViewController:myView animated:YES];
}

- (IBAction)shareData:(id)sender {
    @weakify(self)
    HttpManagerCenter *httpService = [HttpManagerCenter sharedHttpManager];
    [[httpService shareToFriendDataRequest:nil] subscribeNext:^(BaseModel *model) {
        @strongify(self)
        if (model.code == 200) {
            [self addShareDataViewToSbWithShareData:model.data[@"share_data"]];
        }else{
            [self showRequestErrorMessage:model];
        }
    }];
}


- (IBAction)commentMaster:(id)sender {
    //去评分
    NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",@"1142063737"];
    NSURL * url = [NSURL URLWithString:str];
    
    if ([[UIApplication sharedApplication] canOpenURL:url])
    {
        [[UIApplication sharedApplication] openURL:url];
    }
    else
    {
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:@"打开AppStore失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
    }
}

- (IBAction)aboutMaster:(id)sender {
    
//    NSString *path = [UserClient sharedUserClient].about_us_url;
//    [self pushViewControllerWithUrl:path];
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]];
    UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"AboutUsViewController"];
    [self pushViewController:myView animated:YES];
    
}

- (IBAction)clearCache:(id)sender {
    NSString *cacheSize = [self getCacheImageSize];
    [ClearCacheTool clearCacheWithFilePath:[self getSavePath]];
    [[SDImageCache sharedImageCache] clearDisk];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    self.cacheSize.text=@"0.00kb";
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"已成功清除%@缓存数据",cacheSize] delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
}



- (IBAction)exitAction:(id)sender {
    
    [self showHUDWithString:@"提交中..."];
    @weakify(self)
    HttpManagerCenter *httpService = [HttpManagerCenter sharedHttpManager];
    [[httpService logoutWithUid:[UserClient sharedUserClient].userId resultClass:nil] subscribeNext:^(BaseModel *model){
    } error:^(NSError *error) {
        
        NSLog(@"error = %@",error);
    } completed:^{
        @strongify(self)
        [[UserClient sharedUserClient] outLogin];
        [SharedAppDelegate openAppMainVCT];
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"stopTimerToUpdate" object:nil];
//        [self doLogin];
//        UIStoryboard *HelpStory = [UIStoryboard storyboardWithName:@"GuardMe" bundle:[NSBundle mainBundle]];
//        BaseViewController * BaseViewController = [HelpStory instantiateViewControllerWithIdentifier:@"CenterAndDeterenceRootVC"];
//        [self presentViewController:BaseViewController animated:NO completion:nil];
        [self hiddenHUDWithString:@"退出成功" error:NO];
        
    }];
}


- (NSString*)getCacheImageSize{
    //    float diskSize = 1.0f*(long)[NSURLCache sharedURLCache].currentDiskUsage;
    float audioSize  =[ClearCacheTool getCacheSizeWithFilePath:[self getSavePath]];
    float diskSize = 1.0f*[SDImageCache sharedImageCache].getSize;
    diskSize = (diskSize + audioSize)/1024.0f;
    if (diskSize>1024) {
        diskSize = diskSize/1024.0f;
        return [NSString stringWithFormat:@"%.2fMb",diskSize];
    }else{
        //        diskSize = 0.0f;
        return [NSString stringWithFormat:@"%.2fKb",diskSize];
    }
}

@end
